package com.whfc.ms.redis.impl;

import com.whfc.ms.redis.UserSessionRedisDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class UserSessionRedisDaoImpl implements UserSessionRedisDao {

    @Autowired
    private StringRedisTemplate redisTemplate;

    private static final String MS_SESSION_KEY = "ms::session";

    @Override
    public void setSessionId(Integer userId, String sessionId) {
        String key = MS_SESSION_KEY;
        redisTemplate.opsForHash().put(key, String.valueOf(userId), sessionId);
    }

    @Override
    public String getSessionId(Integer userId) {
        String key = MS_SESSION_KEY;
        Object val = redisTemplate.opsForHash().get(key, String.valueOf(userId));
        return String.valueOf(val);
    }
}
